package cn.aigoubuy.dao.ilpl;

import cn.aigoubuy.dao.OrderDao;
import cn.aigoubuy.domain.Order;
import cn.aigoubuy.domain.OrderItem;
import cn.aigoubuy.domain.Product;
import cn.aigoubuy.domain.User;
import cn.aigoubuy.utils.JDBCUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
* 订单dao实现类
*/
public class OrderDaoImpl implements OrderDao {
    @Override
    public void saveOrder(Connection connection, Order order) throws SQLException {
        String sql="INSERT INTO orders VALUES(?,?,?,?,?,?,?,?)";
        QueryRunner qr=new QueryRunner();
        Object[] params= {order.getOid(),order.getOrdertime(),order.getTotal(),order.getState(),order.getAddress(),order.getName(),order.getTelephone(),order.getUser().getUid()};
        qr.execute(connection,sql,params);
    }
    @Override
    public void saveOrderItem(Connection connection, OrderItem item) throws SQLException {
        String sql="INSERT INTO orderitem VALUES(?,?,?,?,?)";
        QueryRunner qr=new QueryRunner();
        Object[] params= {item.getItemid(),item.getQuantity(),item.getSubtotal(),item.getProduct().getPid(),item.getOrder().getOid()};
        qr.execute(connection,sql,params);
    }

    @Override
    public int findTotalRecordByUid(User user) throws SQLException {
        QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
        String sql = "select count(*) from orders where uid = ?";
        Long count = (Long)qr.query(sql,new ScalarHandler<>(),user.getUid());
        return count.intValue();
    }
    @Override
    public List<Order> findAllByUid(User user, int startIndex, int pageSize)
            throws Exception {
        QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
        String sql = "select * from orders where uid = ? order by ordertime desc limit ?,?";
        final List<Order> list = qr.query(sql, new BeanListHandler<>(Order.class), user.getUid(), startIndex, pageSize);
        //遍历获得每个订单，获取每个订单的订单项
        for (Order order : list) {
            // 进行查询单个订单的订单项
            sql = "select * from orderitem o,product p where oid = ? and o.pid=p.pid";
            final List<Map<String, Object>> oList = qr.query(sql, new MapListHandler(), order.getOid());
            for (Map<String, Object> map : oList) {
                // 将属于订单项的封装到OrderItem中
                OrderItem orderItem = new OrderItem();
                BeanUtils.populate(orderItem,map);
                //将属于商品的数据封装到Product中
                Product product = new Product();
                BeanUtils.populate(product,map);
                orderItem.setProduct(product);
                orderItem.setOrder(order);
                order.getList().add(orderItem);
            }
            order.setUser(user);
        }
        return list;
    }

    @Override
    public Order findByOid(String oid) throws Exception {
        QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
        String sql = "select * from orders where oid = ?";
        Order order = qr.query(sql, new BeanHandler<>(Order.class), oid);
        //封装order中list集合的数据
        sql = "select * from orderitem o,product p where oid = ? and o.pid=p.pid";
        final List<Map<String, Object>> oList = qr.query(sql, new
                MapListHandler(), order.getOid());
        for (Map<String, Object> map : oList) {
            // 将属于订单项的封装到OrderItem中
            OrderItem orderItem = new OrderItem();
            BeanUtils.populate(orderItem,map);
            //将属于商品的数据封装到Product中
            Product product = new Product();
            BeanUtils.populate(product,map);
            orderItem.setProduct(product);
            orderItem.setOrder(order);
            order.getList().add(orderItem);
        }
        return order;
    }

    @Override
    public void update(Order order) throws Exception {
        QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
        String sql = "update orders set total =?,state =?,address =?,name =?,telephone =?where oid = ?";
        Object[] params =
                {order.getTotal(), order.getState(), order.getAddress(),
                        order.getName(), order.getTelephone(), order.getOid()};
        qr.update(sql, params);
    }

}